//
//  TSCellMoveCollectionView+Move.m
//  TSDemo_List
//
//  Created by 李超前 on 2020/11/14.
//

#import "TSCellMoveCollectionView+Move.h"
#import <UICollectionViewCJHelper/UICollectionView+CJLongPressMove.h>

@implementation TSCellMoveCollectionView (Move)

#pragma mark - Event
/*
*  移除拖动手势Move
*/
- (void)removeMoveGR {
    [self cj_removeLongPressMoveGR];
}

/*
*  添加拖动手势Move
*
*  @param cellMoveErrorBlock         移动整个过程出现错误的回调
*/
- (void)addMoveGRWithCellMoveErrorBlock:(void(^ __nullable)(NSString *erorMessage))cellMoveErrorBlock {
    [self cj_addLongPressMoveGR:0.1];
    
    [self cj_setupCheckSectionMoveEnableBlock:^BOOL(NSIndexPath * _Nonnull indexPath) {
        return YES;
    } checkCellMoveEnableBlock:^BOOL(NSInteger fromSection, NSInteger toSection) {
        return YES;
    } cellMoveCompleteBlock:^(NSIndexPath * _Nonnull sourceIndexPath, NSIndexPath * _Nonnull destinationIndexPath) {
        //从数据源中移除该数据
        id dataModel = [self.dataModels objectAtIndex:sourceIndexPath.item];
        [self.dataModels removeObject:dataModel];
        //并将数据插入到数据源中目标位置
        [self.dataModels insertObject:dataModel atIndex:destinationIndexPath.row];
        
        NSString *message = @"Cell移动完毕";
        !cellMoveErrorBlock ?: cellMoveErrorBlock(message);
    }];
}

@end
